package moodish.dummy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import moodish.comm.ServerSideMessage.Type;

/**
 * Thread class for Client comunication class. For each Client,
 * there is a DealServer who will treat the received messages.
 * A DealServer contains an ObjectOutputStream, an ObjectInputStream, a Socket, a ClientCommDummy
 * and a nickname(String).
 * 
 * @author group27
 * @version 1
 */

public class DealWithServerMessages extends Thread{
	
	private Socket socket;
	private ClientCommDummy clientcomm;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private String nickname;

	/**
	 * Constructor.
	 * @param Socket socket of connection estabilished.
	 * @param ClientComm comunication client class.
	 * @param nickname nickname of client.
	 * 
	 * @throws IOException            Thrown if a connection cannot be established.
	 */
	
	public DealWithServerMessages(Socket socket, ClientCommDummy clientcomm, String nickname) {
		this.socket=socket;
		this.clientcomm=clientcomm;
		this.nickname=nickname;
		try {
			out = new ObjectOutputStream(socket.getOutputStream());
			in = new ObjectInputStream(socket.getInputStream());
			out.writeObject(nickname);
		} catch (IOException e) {
			
		}
	}
	
	/**
	 * Receives messages from server and sends them to ClientComm.
	 * 
	 * @throws ClassNotFoundException   Thrown if the Class cannot be found.
	 * @throws IOException            Thrown if a connection cannot be established.
	 */
	
	public void run(){
		while(true){
			ClienteSideMessage message = null;
			try {
				message = (ClienteSideMessage) in.readObject();
				clientcomm.addMessage(message);
			} catch (ClassNotFoundException e) {
				
			} catch (IOException e) {
				
			}
		}
	}
	/**
	 * Request to unfriend of  a client.
	 * 
	 * @param nickname the nickname of the client currently friend that should be unfriended.
	 * @throws IOException            Thrown if a connection cannot be established. 
	 */
	
	public void unfriendship(String nickname) {
		ServerSidesMessage unfriend = new ServerSidesMessage(this.nickname, nickname, Type.UNFREINDSHIP);
		try {
			out.writeObject(unfriend);
		} catch (IOException e) {
			
		}
		
	}
	
	/**
	 * Send a Moodish message from this client to the server which should then relay the message to all freinds.
	 * 
	 * @param moodishMessage The Moodish message to be sent.
	 * @throws IOException            Thrown if a connection cannot be established. 
	 */

	public void sendMoodishMessage(String moodishMessage) {
		ServerSidesMessage moodishmessage = new ServerSidesMessage(this.nickname, moodishMessage, Type.MOODISH_MESSAGE);
		try {
			out.writeObject(moodishmessage);
		} catch (IOException e) {
			
		}
		
	}
	
	/**
	 * Sends a Connected message from this cliente to the server.
	 * 
	 *
	 */
	
	public void sendConnectedMessage(){
		ServerSidesMessage connectedmessage = new ServerSidesMessage(this.nickname, null, Type.CLIENT_CONNECTED);
		try {
			out.writeObject(connectedmessage);
		} catch (IOException e) {
			
		}
	}
	
	/**
	 * Sends a Disconnected message from this cliente to the server.
	 * 
	 *
	 */
	
	public void sendDisconnectedMessage(){
		ServerSidesMessage disconnectedmessage = new ServerSidesMessage(this.nickname, null, Type.CLIENT_DISCONNECTED);
		try {
			out.writeObject(disconnectedmessage);
		} catch (IOException e) {
			
		}
	}
	
	
	/**
	 * Request to be friend of another client.
	 * 
	 * @param nickname the nickname of the client that should be friend.
	 * @throws IOException            Thrown if a connection cannot be established. 
	 */

	public void friendship(String nickname) {
		ServerSidesMessage friend = new ServerSidesMessage(this.nickname, nickname, Type.FRIENDSHIP);
		try {
			out.writeObject(friend);
		} catch (IOException e) {
			
		}
		
	}
	
	/**
	 * Checks if is connected.
	 * 
	 * 
	 * @return <b>true</b> if connected, otherwise <b>false</b>.
	 */
	
	public boolean isConnected(){
		return socket.isClosed();
	}

	


}
